﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using SARS.Biz;
using SARS.Biz.Facades;
using SARS.Entity;

namespace SARS.Tutor
{
    public partial class AddSession : Page
    {
        private readonly IRoomBO _roomBO = new RoomBO();
        private readonly ISessionBO _sessionBO = new SessionBO();
        private readonly IUserBO _userBO = new UserBO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Roles"] == null || RoleType.Coordinator != (RoleType)Session["Roles"])
            {
                Response.Write("<script language='javascript'>window.alert('Your role is not allow to access this page.');" +
                               "window.location='../Account/Login.aspx';</script>");
            }

            if (!IsPostBack)
            {
                ddlManager.DataSource = GetTutorLecture(); //_userBO.RetrieveUserByRoleId((int)RoleType.Tutor);
                ddlManager.DataBind();

                ddlRoom.DataSource = _roomBO.RetrieveAllRoom();
                ddlRoom.DataBind();

                dtpStartDateTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm");
                dtpEndtDateTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm");
            }
        }

        protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private List<UserEntity> GetTutorLecture()
        {
            List<UserEntity> userList = _userBO.RetrieveUserByRoleId((int) RoleType.Tutor).ToList();
            userList.AddRange(_userBO.RetrieveUserByRoleId((int) RoleType.Lecturer));

            return userList;
        }

        protected void ddlManager_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddlRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void btnAddSession_Click(object sender, EventArgs e)
        {
            if (Page.IsValid && !string.IsNullOrEmpty(dtpStartDateTime.Text) &&
                !string.IsNullOrEmpty(dtpEndtDateTime.Text) && !string.IsNullOrEmpty(txtSessionTopic.Text))
            {
                var session = new SessionEntity
                {
                    SessionTopic = txtSessionTopic.Text,
                    SessionType = ddlSessionType.SelectedValue,
                    StartDate = Convert.ToDateTime(dtpStartDateTime.Text),
                    EndDate = Convert.ToDateTime(dtpEndtDateTime.Text),
                    CourseID = UcProgramCourse1.SelectedCourseValue,
                    ManagerID = Int32.Parse(ddlManager.SelectedValue),
                    RoomID = Int32.Parse(ddlRoom.SelectedValue),
                    Status = (int) SessionType.Unfinished
                };

                //session.Manager = (TutorEntity)_userBO.RetrieveUserById(session.ManagerID);

                int result = _sessionBO.InsertSession(session);

                if (result > 0)
                {
                    Response.Redirect("SessionList.aspx");
                    Response.Write("<script>alert('Create session sucessfully!')</script");
                }
                else
                {
                    Response.Write("<script>alert('There are no assigned students to selected course!')</script");
                }
            }
        }


        protected void btnStudentAttendance_Click(object sender, EventArgs e)
        {
            Response.Redirect("StudentAttendance.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }
    }
}